package com.zheng.blogsystem.pointtestclient;

import com.zheng.blogsystem.pointtestclient.base.Constants;
import com.zheng.blogsystem.pointtestclient.okhttp.OkHttpUtil;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import okhttp3.Response;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.*;

import javax.swing.*;
import javax.swing.plaf.nimbus.State;

@SwingContainer
public class LoginController {
    @FXML
    private Label loginTips;
    @FXML
    private TextField usernameFd;
    @FXML
    private PasswordField passwordFd;

    @FXML
    private Button forgetPasswordBtn;
    @FXML
    private Button registerUserBtn;


    @FXML
    protected void onLoginButtonClick() {

        String usernameFdText = usernameFd.getText();
        String passwordFdText = passwordFd.getText();
        if (usernameFdText.isEmpty()) {
            loginTips.setText("用户名不能为空");
            return;
        }
        if (passwordFdText.isEmpty()) {
            loginTips.setText("账户密码不能为空");
            return;
        }

        //登陆操作
        try {
//                User user = new User();
//                user.setName(usernameFdText);
//                user.setPswd(passwordFdText);
            Map<String, String> map = new HashMap<>();
            map.put("name", usernameFdText);
            map.put("password", passwordFdText);
            String requestJson = new Gson().toJson(map);
            System.out.println("requestJson:" + requestJson);
            Response response = OkHttpUtil.getInstance().postRequest(Constants.POINT_LOGIN, requestJson);
            assert response.body() != null;
            String responseString = response.body().string();
            System.out.println("responseString:" + responseString);
            loginTips.setText("恭喜! 登陆成功");

        } catch (IOException e) {
            loginTips.setText("抱歉! 登陆失败");
            e.printStackTrace();
        }

    }

    public void onFindPasswordButtonClick() {
        //跳转到找回密码页面

    }

    public void onRegisterUserButtonClick() {
        //跳转到用户注册界面
        Stage state = (Stage) registerUserBtn.getScene().getWindow();
        state.close();
        Application.pageChange("register.fxml", "用户注册窗口");
    }
}